package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.SealUseAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseOriginal;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.MyConst;
import com.isunland.managesystem.entity.BaseParams;
import com.isunland.managesystem.entity.DataStatus;
import com.isunland.managesystem.entity.RSealsUseApp;
import com.isunland.managesystem.entity.SealUseDetailParams;
import com.isunland.managesystem.entity.SealUseQueryParams;
import com.isunland.managesystem.utils.MenuUtil;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/6/13 16:16
 */
public class SealUseListFragment extends BaseListFragment {
  private static final int REQUEST_QUERY = 1;
  private static final int REQUEST_DETAIL = 2;
  private static final int REQUEST_ADD = 3;
  private static final int MENU_ITEM_ADD = 4;
  private static final int MENU_ITEM_SEARCH = 5;
  private SealUseQueryParams mParams;
  private ArrayList<RSealsUseApp> mList;
  private SealUseAdapter mAdapter;

  @Override public void initData() {
    super.initData();
    mParams = (!(mBaseParams instanceof SealUseQueryParams)) ? new SealUseQueryParams()
        : (SealUseQueryParams) mBaseParams;
    //默认筛选条件
    mParams.setApplyDeptStartDate(MyDateUtil.getBeforeMonth(1));
    mParams.setApplyDeptStopDate(new Date());
    mParams.setSealsTypeCode(MyConst.EMPTY);
    mParams.setSealsTypeName(getString(R.string.all));
    mParams.setDataStatus(MyConst.EMPTY);
    mParams.setDataStatusName(getString(R.string.all));
    mParams.setIsCarryOutCode(MyConst.EMPTY);
    mParams.setIsCarryOutName(getString(R.string.noLimit));

    mList = new ArrayList<>();
    mAdapter = new SealUseAdapter(mActivity, mList);
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    setListAdapter(mAdapter);
    setTitleCustom(R.string.sealUse);
  }

  @Override public String getUrl() {
    return ApiConst.URL_QUERY_LIST_SEALUSE;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("beginQueryregDate",
        MyDateUtil.format(mParams.getApplyDeptStartDate(), MyDateUtil.TYPE_DATE));
    params.put("endQueryregDate",
        MyDateUtil.format(mParams.getApplyDeptStopDate(), MyDateUtil.TYPE_DATE));
    params.put("dataStatus", mParams.getDataStatus());
    params.put("isCarryOut", mParams.getIsCarryOutCode());
    params.put("sealsTypeCode", mParams.getSealsTypeCode());
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    BaseOriginal<RSealsUseApp> result =
        new Gson().fromJson(json, new TypeToken<BaseOriginal<RSealsUseApp>>() {
        }.getType());
    mList.clear();
    mList.addAll(result.getRows());
    mAdapter.notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    if (position < mListview.getHeaderViewsCount()) return;
    RSealsUseApp item = mAdapter.getItem(position - l.getHeaderViewsCount());
    if (item == null) return;
    SealUseDetailParams params = new SealUseDetailParams();
    params.setType(BaseParams.TYPE_DETAIL);
    params.setTitle(MyStringUtil.connect(R.string.sealUse, R.string.detail1));
    params.setItem(item);
    params.setShowMenuAlter(true);
    params.setShowMenuRun(true);
    params.setShowMenuStop(true);
    params.setShowMenuDelete(true);
    SealUseDetailActivity.newInstance(this, SealUseDetailActivity.class, params, REQUEST_DETAIL);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    MenuUtil.addSubMenu(menu, MENU_ITEM_ADD, R.string.add1)
        .setIcon(R.drawable.add)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    MenuUtil.addSubMenu(menu, MENU_ITEM_SEARCH, R.string.query)
        .setIcon(R.drawable.search)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    if (MENU_ITEM_SEARCH == item.getItemId()) {
      BaseVolleyActivity.newInstance(this, SealUseQueryActivity.class, mParams, REQUEST_QUERY);
    }
    if (MENU_ITEM_ADD == item.getItemId()) {
      SealUseDetailParams params = new SealUseDetailParams();
      params.setType(BaseParams.TYPE_ADD);
      params.setTitle(MyStringUtil.connect(R.string.sealUse, R.string.add1));
      RSealsUseApp data = new RSealsUseApp();
      data.setId(UUID.randomUUID().toString());
      data.setRegStaffId(mCurrentUser.getJobNumber());
      data.setRegStaffName(mCurrentUser.getRealName());
      data.setRegDate(new Date());
      data.setDataStatus(DataStatus.NEW);
      data.setMemberCode(mCurrentUser.getMemberCode());
      data.setApplyNo(String.valueOf(MyDateUtil.getTimestamp()));
      data.setApplydeptName(mCurrentUser.getDeptName());
      data.setApplydeptCode(mCurrentUser.getDeptCode());
      data.setApplydeptDate(new Date());

      params.setItem(data);
      params.setShowMenuConfirm(true);
      params.setShowMenuRun(true);
      SealUseDetailActivity.newInstance(this, SealUseDetailActivity.class, params, REQUEST_ADD);
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == REQUEST_QUERY && data != null) {
      mParams = (SealUseQueryParams) data.getSerializableExtra(BaseQueryFragment.EXTRA_PARAMS);
      volleyPost();
    }
    if (requestCode == REQUEST_DETAIL) {
      volleyPost();
    }
    if (requestCode == REQUEST_ADD) {
      volleyPost();
    }
  }
}
